package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.po.ExperimentProcessSpecimen;
import com.zhijian.medical.entity.vo.request.*;
import com.zhijian.medical.entity.vo.response.*;
import com.zhijian.medical.service.*;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 实验流程 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-04-06 22:09:16
 */
@Slf4j
@Controller
@Api(tags = "实验流程")
public class ExperimentProcessController {

    @Autowired
    private ExperimentProcessService service;

    @Autowired
    private ExperimentProcessDetailService experimentProcessDetailService;

    @Autowired
    private ExperimentProcessCsvService experimentProcessCsvService;

    @Autowired
    private ExperimentProcessResultService experimentProcessResultService;

    @Autowired
    private ExperimentProcessSpecimenService experimentProcessSpecimenService;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/experiment/process")
    @ApiOperation("新增")
    public boolean insert(@RequestBody ExperimentProcessSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/experiment/process/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/experiment/process/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody ExperimentProcessSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/experiment/process/{id}")
    @ApiOperation("根据id查询")
    public ExperimentProcessVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/experiment/process/page")
    @ApiOperation("分页查询")
    public Page<ExperimentProcessVo> selectByCondition(@RequestBody(required = false) ExperimentProcessSearchVo param) {
        if (param == null)
            param = ExperimentProcessSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 新增or修改实验详情
     *
     * @param id
     * @param experimentProcessDetailSaveOrUpdateVo
     * @return
     */
    @Response
    @PostMapping("/experiment/process/detail/{id}")
    @ApiOperation("新增or修改实验详情")
    public boolean saveOrUpdateDetail(@PathVariable("id") Long id, @RequestBody ExperimentProcessDetailSaveOrUpdateVo experimentProcessDetailSaveOrUpdateVo) {
        experimentProcessDetailSaveOrUpdateVo.setExperimentProcessId(id);
        return experimentProcessDetailService.saveOrUpdate(experimentProcessDetailSaveOrUpdateVo);
    }

    /**
     * 根据id查询实验流程详情
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/experiment/process/detail/{id}")
    @ApiOperation("根据id查询实验流程详情")
    public ExperimentProcessDetailVo selectVoByExperimentProcessId(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return experimentProcessDetailService.selectVoByExperimentProcessId(id);
    }

    /**
     * 新增或者修改csv数据
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/experiment/process/csv/{id}")
    @ApiOperation("新增或者修改csv数据")
    public boolean insertOrUpdateCsv(@PathVariable("id") Long id, @RequestBody ExperimentProcessCsvSaveOrUpdateVo param) {
        param.setExperimentProcessId(id);
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return experimentProcessCsvService.insertOrUpdate(param);
    }

    /**
     * 列表查询csv数据
     *
     * @return
     */
    @Response
    @PostMapping("/experiment/process/csv/list/{id}")
    @ApiOperation("列表查询csv数据")
    public List<ExperimentProcessCsvVo> selectByCondition(@PathVariable("id") Long id) {
        return experimentProcessCsvService.selectListByExperimentProcessId(id);
    }

    /**
     * 新增或者修改结果判定数据
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/experiment/process/result/{id}")
    @ApiOperation("新增或者修改结果判定数据")
    public boolean insertOrUpdateResult(@PathVariable("id") Long id, @RequestBody ExperimentProcessResultSaveOrUpdateVo param) {
        param.setExperimentProcessId(id);
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return experimentProcessResultService.insertOrUpdate(param) != 0;
    }

    /**
     * csv文件导入
     *
     * @param experimentProcessId
     * @param file
     * @return
     */
    @Response
    @PostMapping("/experiment/process/csv/import/{experimentProcessId}")
    @ApiOperation("csv文件导入")
    public List<ExperimentProcessCsvVo> importCsv(@ApiParam(name = "experimentProcessId", value = "实验流程id") @RequestParam(value = "experimentProcessId") Long experimentProcessId, MultipartFile file) {
        return experimentProcessCsvService.importCsv(experimentProcessId, file);
    }

    /**
     * 查询临床标本标识和扩增位置数据
     *
     * @return
     */
    @Response
    @GetMapping("/experiment/process/specimen/{id}")
    @ApiOperation("查询临床标本标识和扩增位置数据")
    public ExperimentProcessSpecimenVo selectExperimentProcessSpecimenVo(@PathVariable("id") Long id) {
        return experimentProcessSpecimenService.selectVoByExperimentProcessId(id);
    }

    /**
     * 新增或者临床标本标识和扩增位置数据
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/experiment/process/specimen/{id}")
    @ApiOperation("新增或者临床标本标识和扩增位置数据")
    public boolean insertOrUpdateSpecimen(@PathVariable("id") Long id, @RequestBody ExperimentProcessSpecimen param) {
        param.setExperimentProcessId(id);
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return experimentProcessSpecimenService.insertOrUpdate(param) != 0;
    }

    /**
     * excel文件导入
     *
     * @param experimentProcessId
     * @param file
     * @return
     */
    @Response
    @PostMapping("/experiment/process/specimen/import/{experimentProcessId}/{deviceId}")
    @ApiOperation("excel文件导入")
    public boolean importExcel(@ApiParam(name = "experimentProcessId", value = "实验流程id")
                               @PathVariable("experimentProcessId") Long experimentProcessId,
                               @ApiParam(name = "deviceId", value = "扩增仪id")
                               @PathVariable("deviceId") Long deviceId,
                               MultipartFile file) {
        return experimentProcessSpecimenService.importExcel(experimentProcessId, deviceId, file);
    }

    /**
     * 获取组装好的临床标本标识和扩增位置数据
     *
     * @param experimentProcessId
     * @return
     */
    @Response
    @GetMapping("/experiment/process/specimen/batch/{experimentProcessId}/{deviceId}")
    @ApiOperation("获取组装好的临床标本标识和扩增位置数据")
    public ExperimentProcessSpecimenBatchVo getExperimentProcessSpecimenBatchVo(@ApiParam(name = "experimentProcessId", value = "实验流程id")
                                                                                @PathVariable("experimentProcessId") Long experimentProcessId,
                                                                                @ApiParam(name = "deviceId", value = "扩增仪id")
                                                                                @PathVariable("deviceId") Long deviceId) {
        return experimentProcessSpecimenService.selectExperimentProcessSpecimenBatchVoByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
    }

    /**
     * 列表查询结果判定数据
     *
     * @return
     */
    @Response
    @PostMapping("/experiment/process/result/list/{experimentProcessId}/{deviceId}")
    @ApiOperation("列表查询结果判定数据")
    public List<ExperimentProcessResultVo> selectListResult(@ApiParam(name = "experimentProcessId", value = "实验流程id")
                                                            @PathVariable("experimentProcessId") Long experimentProcessId,
                                                            @ApiParam(name = "deviceId", value = "扩增仪id")
                                                            @PathVariable("deviceId") Long deviceId) {
        return experimentProcessResultService.selectList(ExperimentProcessResultSearchVo.builder()
                .experimentProcessId(experimentProcessId)
                .deviceId(deviceId)
                .build());
    }

    /**
     * 生成实验报告
     *
     * @param experimentProcessId
     * @return
     */
    @Response
    @PostMapping("/experiment/process/create/report/{experimentProcessId}")
    @ApiOperation("生成实验报告")
    public boolean createReport(@ApiParam(name = "experimentProcessId", value = "实验流程id")
                                @PathVariable("experimentProcessId") Long experimentProcessId) {
        return service.createReport(experimentProcessId);
    }

    /**
     * 修改实验报告的检测日期
     *
     * @param experimentProcessId
     * @param date
     * @return
     */
    @Response
    @PutMapping("/experiment/process/detail/date/{experimentProcessId}")
    @ApiOperation("修改实验报告的检测日期")
    public boolean updateDate(@ApiParam(name = "experimentProcessId", value = "实验流程id")
                              @PathVariable("experimentProcessId") Long experimentProcessId,
                              @ApiParam(name = "date", value = "检验日期，格式为'2022-11-11'")
                              @RequestParam(value = "date") String date) {
        log.info("修改实验报告的检测日期,the param is {},{}", experimentProcessId, date);
        return experimentProcessDetailService.updateDate(experimentProcessId, date);
    }

    /**
     * 获取实验报告的检测日期
     *
     * @param experimentProcessId
     * @return
     */
    @Response
    @GetMapping("/experiment/process/detail/date/{experimentProcessId}")
    @ApiOperation("获取实验报告的检测日期")
    public String getDate(@ApiParam(name = "experimentProcessId", value = "实验流程id")
                          @PathVariable("experimentProcessId") Long experimentProcessId) {
        ExperimentProcessDetailVo experimentProcessDetail = experimentProcessDetailService.selectVoByExperimentProcessId(experimentProcessId);
        return Objects.nonNull(experimentProcessDetail) ? experimentProcessDetail.getDate() : null;
    }

}